package com.example.communitysystem.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class PaginationDTO<T> {
    private List<T> dataDTOList;      //存放分页数据
    private boolean showFirstPage;                  //是否有前一页
    private boolean showPreviousPage;               //是否展示上一页
    private boolean showEndPage;                    //是否有下一页
    private boolean showNextPage;                   //是否展示下一页
    private Integer page;                           //当前页
    private List<Integer> pages = new ArrayList<>();//存放页数的集合
    private Integer totalPage;

    public void setPagination(Integer totalPage, Integer page) {
        this.totalPage = totalPage;
        this.page = page;

        //计算当前页码数前后相邻的3个页数，总的页数集合为7
        pages.add(page);
        //123
        for(int i = 1; i <= 3; i++){
            if(page - i > 0){
                pages.add(0,page-i);
            }
            if(page + i <= totalPage){
                pages.add(page+i);
            }
        }

        //是否展示上一页
        if(page == 1){
            showPreviousPage = false;
        }else{
            showPreviousPage = true;
        }
        //是否展示下一页
        if(page == totalPage){
            showNextPage = false;
        }else{
            showNextPage = true;
        }
        //是否展示第一页
        if(pages.contains(1)){
            showFirstPage = false;
        }else{
            showFirstPage = true;
        }

        //是否展示最后一页
        if(pages.contains(totalPage)){
            showEndPage = false;
        }else{
            showEndPage = true;
        }

    }

}
